package van.planifolia.staticproxy;

import van.planifolia.util.MakeLog;

/**
 * Created by Intellij IDEA<br>
 *
 * @author Planifolia.Van
 * @version 1.0
 * @date 2022/11/9 16:35
 */
public class ComputerProxy implements Computer{
    // 引入被代理类
    private ComputerImpl computer;
    // 使用构造器注入被代理类
    public ComputerProxy(ComputerImpl computer) {
        this.computer = computer;
    }
    // 在这下面的代理方法中对被代理类的方法进行增强操作
    @Override
    public int add(int a, int b) {
        // 加法没啥好校验的任何数都可以相加
        System.out.println("校验通过");
        int add = computer.add(a, b);
        // 记录日志
        MakeLog.makeLog(a + "+" + b + "=" + add);
        return add;
    }

    @Override
    public int reduce(int a, int b) {
        // 减法没啥好校验的任何数都可以相加
        System.out.println("校验通过");
        int reduce = computer.reduce(a, b);
        MakeLog.makeLog(a + "-" + b + "=" + reduce);
        return reduce;
    }

    @Override
    public int multiply(int a, int b) {
        // 乘法没啥好校验的任何数都可以相加
        System.out.println("校验通过");
        int multiply = computer.multiply(a, b);
        MakeLog.makeLog(a + "*" + b + "=" + multiply);
        return multiply;
    }

    @Override
    public int divide(int a, int b) {
        // 除法要校验被除数是否为0
        if (b==0){
            throw new RuntimeException("被除数不得为0！");
        }else {
            System.out.println("校验通过");
            int divide = computer.divide(a, b);
            MakeLog.makeLog(a + "/" + b + "=" + divide);
            return divide;
        }
    }
}
